// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <boost/random/uniform_int_distribution.hpp>
#include <pollux/plan/input_generator.h>
using namespace kumo::pollux::plan;

namespace kumo::pollux::exec::test {

class ClassificationAggregationInputGenerator : public InputGenerator {
 public:
  std::vector<VectorPtr> generate(
      const std::vector<TypePtr>& types,
      VectorFuzzer& fuzzer,
      FuzzerGenerator& rng,
      memory::MemoryPool* pool) override {
    std::vector<VectorPtr> result;
    result.reserve(types.size());

    POLLUX_CHECK(
        types.size() == 3 || types.size() == 4,
        fmt::format("Unexpected types count:{}", types.size()));

    POLLUX_CHECK(
        types[0]->is_bigint(), "Unexpected type: {}", types[0]->toString());
    POLLUX_CHECK(
        types[2]->is_double(), "Unexpected type: {}", types[2]->toString());

    /// The bucket must be the same everytime or else classification
    /// aggregation function considers this an invalid input. Moreover, the
    /// buckets are capped to 50'000 to prevent OOM-ing issues. The minimum is
    /// 2 since that is the minimum valid bucket count.
    if (!bucket_.has_value()) {
      bucket_ = boost::random::uniform_int_distribution<int64_t>(2, 500)(rng);
    }
    const auto size = fuzzer.getOptions().vectorSize;
    pollux::VectorMaker vectorMaker{pool};
    auto bucket = vectorMaker.flat_vector<int64_t>(
        size, [&](auto /*row*/) { return bucket_.value(); });

    auto pred = vectorMaker.flat_vector<double>(size, [&](auto /*row*/) {
      /// Predictions must be > 0.
      return std::uniform_real_distribution<double>(
          0, std::numeric_limits<double>::max())(rng);
    });

    result.emplace_back(std::move(bucket));
    result.emplace_back(fuzzer.fuzz(types[1]));
    result.emplace_back(std::move(pred));
    if (types.size() == 4) {
      POLLUX_CHECK(
          types[3]->is_double(), "Unexpected type: {}", types[3]->toString());

      auto weight = vectorMaker.flat_vector<double>(size, [&](auto /*row*/) {
        /// Weights must be > 0.
        return std::uniform_real_distribution<double>(
            0, std::numeric_limits<double>::max())(rng);
      });
      result.emplace_back(std::move(weight));
    }
    return result;
  }

  void reset() override {
    bucket_.reset();
  }

 private:
  std::optional<int64_t> bucket_;
};

} // namespace kumo::pollux::exec::test
